package com.chat.spring.repository;

import com.chat.spring.entity.ChatInfo;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class MemoryChatHistoryRepository implements ChatHistoryRepository{
    private Map<String,String> chatInfos = new LinkedHashMap<>();

    /**
     * 保存会话记录
     * 如果是新会话，则新增
     * 如果会话已经存在，则更新
     * @param chatId
     * @param title
     */
    @Override
    public void save(String chatId, String title) {
        chatInfos.put(chatId,title);
    }

    @Override
    public void clearByChatId(String chatId) {
        chatInfos.remove(chatId);
    }

    @Override
    public List<ChatInfo> getChats() {
        return chatInfos.entrySet().
                stream().
                map(entry-> new ChatInfo(entry.getKey(), entry.getValue())).
                collect(Collectors.toList());
    }
}
